/*
 * Author: Jaime Francisco Jaramillo
 * ID: 9272518
 * Date: 22/3/2012
 * Last modified: 22/3/2012
 * 
 */

package taskManagerSystem.ViewSubsystem;


import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import taskManagerSystem.ModelSubsystem.Task;
/*
 * Author: Jaime Francisco Jaramillo
 * ID: 9765727
 * Date: 9/3/2012
 * Last modified: 22/3/2012
 * 
 */

import java.awt.Color;
import java.awt.GridLayout;
import java.util.Vector;

public class TreeView extends JPanel  
{
	// members
    private JTree tree;
    private DefaultMutableTreeNode rootNode;
    private Vector<Task> data;
    
    public TreeView() 
    {
        super(new GridLayout(1,0));
        // Initialize data vector
        data = new Vector<Task>();

        // Create the root node
        rootNode = new DefaultMutableTreeNode(new TaskInfoPair(0, "Tasks"));
         
        // create child nodes
        createNodes(rootNode);

        // Create a tree that allows one selection at a time.
        tree = new JTree(rootNode);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        // hide the root node by default
        tree.setRootVisible(false);

        // Create the scroll pane and add the tree to it. 
        JScrollPane treeView = new JScrollPane(tree);
        add(treeView);
        
        treeView.setBorder(new EmptyBorder(5, 5, 5, 5) );
        this.setBorder(new LineBorder(Color.GRAY, 1) );
    }
    
    private class TaskInfoPair
    {
    	private int id;
    	private String taskName;
    	
    	public TaskInfoPair(int id, String taskName)
    	{
    		this.id = id;
    		this.taskName = taskName;
    	}
    	
    	public String toString()
    	{
			return taskName;
    	}
    }

    // recursive method that looks for all subtasks for a given task (parent node)
	private void createNodes(DefaultMutableTreeNode parentNode) 
	{
		// stopping condition
	    if(data.isEmpty())
	    	return;
	   
    	// look for children
    	for(int i=0; i<data.size(); i++)
	    {
			if(data.elementAt(i).getParentTaskId() == ((TaskInfoPair)parentNode.getUserObject()).id)
    		{
				DefaultMutableTreeNode childTask = new DefaultMutableTreeNode(new TaskInfoPair(data.elementAt(i).getTaskID(), data.elementAt(i).getTitle()));
				parentNode.add(childTask);
    			data.remove(i);
    			i--;
    			createNodes(childTask);
    		}
		}
	     
	}
	
    // cleans up the data vector, the root node and refreshes the UI
    public void  clearData()
    {
    	this.data = null;
    	rootNode.removeAllChildren();
    	tree.collapseRow(0);
    	tree.setRootVisible(false);
    	tree.updateUI();
    }
    
    // takes and array containing the Task information
	public void setData( Vector<Task> data )
	{
		clearData();
		this.data = (Vector<Task>) data.clone();
		
	}
	
	// recreates nodes if necessary and refreshes the tree
	public void update()
	{
		if( !data.isEmpty() )
		{
			createNodes(rootNode);
			tree.setRootVisible(true);
			tree.expandRow(0);
		}
		
		tree.updateUI();
	}
	
	public Vector<Task> getData(){
		return data;
	}
	
	public DefaultMutableTreeNode getRootNode(){
		return rootNode;
	}
	
	public JTree getTree(){
		return tree;
	}
	
}